package com.xb.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xb.entity.Role;
import com.xb.entity.Tree;
import com.xb.mapper.RoleMapper;
import com.xb.mapper.RoleMenuMapper;
import com.xb.service.IRoleService;
import com.xb.util.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Service业务层处理
 */
@Service
public class RoleServiceImp implements IRoleService {
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private RoleMenuMapper roleMenuMapper;

    /**
     * 分页查询列表
     *
     * @param role
     * @return
     */
    @Override
    public Result selectRoleList(Role role, Integer page, Integer limit) {
        Result result = new Result();
        PageHelper.startPage(page, limit);
        List<Role> list = roleMapper.selectRoleList(role);
        PageInfo<Role> rolePageInfo = new PageInfo<>(list);
        if (rolePageInfo.getList() != null) {
            result.setData(rolePageInfo.getList());
            result.setMessage("查询成功");
            result.setTotal(rolePageInfo.getTotal());
            result.setStatus(Result.RESPONSE_SUCCESS);
        }
        return result;
    }

    @Transactional
    @Override
    public Result add(Role role) {
        Result result = new Result();
        try {
            if (roleMapper.selectRoleByRole(role) != null) {
                throw new IllegalAccessException("此角色标识已存在！");
            }
            if (roleMapper.insertRole(role) == 0) {
                throw new IllegalAccessException("角色添加失败！");
            }
            if (role.getMenuIds().isEmpty()) {
                result.setStatus(Result.RESPONSE_SUCCESS);
                result.setMessage("添加成功");
                return result;
            }
            if (roleMenuMapper.insertRoleMenuByRole(role) == 0) {
                throw new IllegalAccessException("菜单添加失败！");
            }
            result.setStatus(Result.RESPONSE_SUCCESS);
            result.setMessage("添加成功");
            return result;
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            result.setMessage(e.getMessage());
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            result.setMessage("ERROR:请联系管理员");
            return result;
        }
    }

    @Transactional
    @Override
    public Result update(Role role) {
        Result result = new Result();
        try {
            if (!role.getRole().equals(roleMapper.selectRoleById(role.getId()).getRole())) {
                if (roleMapper.selectRoleByRole(role) != null) {
                    throw new RuntimeException("此角色标识已存在！");
                }
            }
            if (roleMapper.updateRole(role) == 0) {
                throw new RuntimeException("角色修改失败！");
            }

            if (!roleMenuMapper.selectRoleMenuByRoleId(role.getId()).isEmpty()) {
                if (roleMenuMapper.deleteRoleMenuByRole(role.getId()) == 0) {
                    throw new RuntimeException("角色菜单删除失败！");
                }
            }
            if (role.getMenuIds().isEmpty()) {
                result.setStatus(Result.RESPONSE_SUCCESS);
                result.setMessage("修改成功");
                return result;
            }
            if (roleMenuMapper.insertRoleMenuByRole(role) == 0) {
                throw new RuntimeException("菜单添加失败！");
            }
            result.setStatus(Result.RESPONSE_SUCCESS);
            result.setMessage("修改成功");
            return result;
        } catch (RuntimeException e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            result.setMessage(e.getMessage());
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            result.setMessage("ERROR:请联系管理员");
            return result;
        }
    }

    @Transactional
    @Override
    public Result del(Integer id) {
        Result result = new Result();
        try {
            if (roleMapper.deleteRoleById(id) == 0) {
                throw new IllegalAccessException("角色删除失败");
            }
            if (!roleMenuMapper.selectRoleMenuByRoleId(id).isEmpty()) {
                if (roleMenuMapper.deleteRoleMenuByRole(id) == 0) {
                    throw new IllegalAccessException("菜单删除失败");
                }
            }
            result.setStatus(Result.RESPONSE_SUCCESS);
            result.setMessage("删除成功");
            return result;

        } catch (IllegalAccessException e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            result.setMessage(e.getMessage());
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            result.setMessage("ERROR:请联系管理员");
            return result;
        }
    }

    /**
     * 获取全部角色
     *
     * @return 结果集
     */
    @Override
    public List<Role> selectAllRole() {
        return roleMapper.selectAllRole();
    }

    @Override
    public List<Role> selectAllRoleByEmail(String email) {
        return roleMapper.selectAllRoleByEmail(email);
    }

    @Override
    public Result getRole() {
        Result result = new Result();
        List<Role> roles = roleMapper.selectRoleList(null);
        List<Tree> treeList = new ArrayList<>();
        Tree tree;
        for (Role role : roles) {
            tree = new Tree();
            tree.setTitle(role.getRole());
            tree.setId(role.getId());
            treeList.add(tree);
        }
        result.setData(treeList);
        return result;
    }
}
